//
// Created by qianyi on 2023/4/28.
//
#include <stdio.h>
#include <string.h>
#include "BzDB_file_operate.h"
#include "BzDB_string_operate.h"


/**
 * 把结构体以二进制的方式写入磁盘
 * @param bin_path
 * @param data
 * @param data_len
 */
void BzDB_write_bin_to_disk(const char* bin_path , void * data , int data_len)
{
    FILE * fp = fopen(bin_path,"wb");
    fwrite(data,data_len,1,fp);
    fclose(fp);
}

/**
 * 从磁盘中读取二进制文件，转换为结构体
 * @param bin_path
 * @param data
 * @param data_len
 */
void BzDB_read_bin_from_disk(const char* bin_path , void *data , int data_len)
{
    FILE * fp = fopen(bin_path,"rb");
    fread(data,data_len,1,fp);
    fclose(fp);
}


/**
 * 按行读取
 * @param fp
 * @param buf
 * @param line_max
 * @return
 */
int BzDB_file_operate_read_line(FILE * fp , char *buf,int line_max)
{

    int line_len = 0; // 文件每行的长度

    char* res = fgets(buf, line_max, fp);
    if(res) {
//        line_len = strlen(buf);
//        // 排除换行符
//        if ('\n' == buf[line_len - 1]) {
//            buf[line_len - 1] = '\0';
//            line_len--;
//            if (0 == line_len) {
//                //空行
////                continue;
//            }
//        }
//        // windos文本排除回车符
//        if ('\r' == buf[line_len - 1]) {
//            buf[line_len - 1] = '\0';
//            line_len--;
//            if (0 == line_len) {
//                //空行
////                continue;
//            }
//        }
        return 1;
        /** 对每行数据(buf)进行处理 **/
    }

    return 0;
}

/**
 * 注：每一行最多1000个字符,
 * 把
 *     char buf[1000];
    BzDB_file_operate_read_content( "./1.txt", buf,1000);
    printf("%s",buf);
 * @param fp
 * @param buf
 * @param line_max  一行最多少个字符
 * @param buf_len
 * @return
 */
int BzDB_file_operate_read_content( const char* file_path, char* buf,int line_max )
{
    buf = memset(buf,0,line_max);
    FILE * fp = fopen(file_path,"r");
    if(fp==NULL){
        return 0;
    }

    char temp[1003] = {0};

    char* res = fgets(temp, 1000, fp);
    strcat(buf,temp);
    while(res){
        memset(temp,0,1000);
        res = fgets(temp , 1000, fp);
        strcat(buf,temp);
    }
    fclose(fp);
    return 1;
}


/**
 * 写入txt文件
 * @param file_path
 * @param msg
 */
void BzDB_file_write_content_to_txt(const char* file_path,const char * msg)
{
    FILE *fp = fopen(file_path,"w");
    if(fp == NULL){
        return ;
    }
    fprintf(fp,msg);
    fclose(fp);
}


// ========================================
// ========================================

#if 1
typedef struct Person_t{
    char name[20];
    int age;
}Person_t;
/**
 * @desc 读取二进制文件
 */
void BzDB_file_operate_demo1()
{
    Person_t person;
    strcpy(person.name,"zhangsan");
    person.age = 20;
    BzDB_write_bin_to_disk("test.bin",&person , sizeof(Person_t));

    BzDB_read_bin_from_disk("test.bin",&person,sizeof(Person_t));
    printf("%s , %d",person.name,person.age);
}

/**
 * @desc 读取txt文本文件
 */
void BzDB_file_operate_demo2()
{
    FILE *fp;
    fp = fopen("./1.txt", "r");
    if (NULL == fp) {
        printf("open %s failed.\n", "./1.txt");
        return ;
    }

    #define LINE_MAX 100
    char buf[LINE_MAX] = {0};
    printf("11**************\n");
    while (BzDB_file_operate_read_line(fp,buf,LINE_MAX)){
        // 去除空格
        BzDB_string_strip(buf,buf);
        printf("%s\n",buf);
    }

    fclose(fp);

}

/**
 * 写入txt文件
 */
void BzDB_file_operate_demo3()
{
    const char * msg = "ccc\nddd\n";
    BzDB_file_write_content_to_txt("./in.txt",msg);
}


#endif
// ========================================
// ========================================
// ========================================